<?php
class Category_model extends MY_Model {
	private $module = 'category';
	private $table = 'category';

	function getsearchContent($limit,$page){
		$this->db->select('*');
		$this->db->limit($limit,$page);
		$this->db->order_by($this->input->post('func_order_by'),$this->input->post('order_by'));
		if($this->input->post('content')!='' && $this->input->post('content')!='type here...'){
			$this->db->where('(`title_vi` LIKE "%'.$this->input->post('content').'%" OR `title_en` LIKE "%'.$this->input->post('content').'%" OR `title_gr` = "'.$this->input->post('content').'")');
		}
		if($this->input->post('dateFrom')!='' && $this->input->post('dateTo')==''){
			$this->db->where('created >= "'.date('Y-m-d 00:00:00',strtotime($this->input->post('dateFrom'))).'"');
		}
		if($this->input->post('dateFrom')=='' && $this->input->post('dateTo')!=''){
			$this->db->where('created <= "'.date('Y-m-d 23:59:59',strtotime($this->input->post('dateTo'))).'"');
		}
		if($this->input->post('dateFrom')!='' && $this->input->post('dateTo')!=''){
			$this->db->where('created >= "'.date('Y-m-d 00:00:00',strtotime($this->input->post('dateFrom'))).'"');
			$this->db->where('created <= "'.date('Y-m-d 23:59:59',strtotime($this->input->post('dateTo'))).'"');
		}
		$query = $this->db->get(PREFIX.$this->table);

		if($query->result()){
			return $query->result();
		}else{
			return false;
		}
	}

	function getTotalsearchContent(){
		$this->db->select('*');
		if($this->input->post('content')!='' && $this->input->post('content')!='type here...'){
			$this->db->where('(`title_vi` LIKE "%'.$this->input->post('content').'%" OR `title_en` LIKE "%'.$this->input->post('content').'%" OR `title_gr` = "'.$this->input->post('content').'")');
		}
		if($this->input->post('dateFrom')!='' && $this->input->post('dateTo')==''){
			$this->db->where('created >= "'.date('Y-m-d 00:00:00',strtotime($this->input->post('dateFrom'))).'"');
		}
		if($this->input->post('dateFrom')=='' && $this->input->post('dateTo')!=''){
			$this->db->where('created <= "'.date('Y-m-d 23:59:59',strtotime($this->input->post('dateTo'))).'"');
		}
		if($this->input->post('dateFrom')!='' && $this->input->post('dateTo')!=''){
			$this->db->where('created >= "'.date('Y-m-d 00:00:00',strtotime($this->input->post('dateFrom'))).'"');
			$this->db->where('created <= "'.date('Y-m-d 23:59:59',strtotime($this->input->post('dateTo'))).'"');
		}
		$query = $this->db->count_all_results(PREFIX.$this->table);

		if($query > 0){
			return $query;
		}else{
			return false;
		}
	}

	function getDetailManagement($id){
		$this->db->select('*');
		$this->db->where('id',$id);
		$query = $this->db->get(PREFIX.$this->table);

		if($query->result()){
			return $query->result();
		}else{
			return false;
		}
	}

	function saveManagement(){
		if($this->input->post('statusAdmincp')=='on'){
			$status = 1;
		}else{
			$status = 0;
		}

		$parent_id = $this->input->post('parentIdAdmincp');
		$current_slug = $this->input->post('slugAdmincp');
		if($this->input->post('hiddenIdAdmincp')==0){
			$data = array(
				'title_vi'=> $this->input->post('titleViAdmincp'),
				'title_en'=> $this->input->post('titleEnAdmincp'),
				'title_gr'=> $this->input->post('titleGrAdmincp'),
				'description_vi' => $this->input->post('descVnAdmincp'),
				'description_en' => $this->input->post('descEnAdmincp'),
				'description_gr' => $this->input->post('descGrAdmincp'),
				'parent_id' =>$parent_id,
				'order_view'=> $this->input->post('orderViewGrAdmincp'),
				'slug'=> $this->input->post('slugAdmincp'),
				'link' => $this->input->post('slugAdmincp'),
				'status'=> $status,
				'created'=> date('Y-m-d H:i:s',time())
			);
			if($this->db->insert(PREFIX.$this->table,$data)){
				//update link
				if($parent_id != 0){
					$result = $this->model->get('*', PREFIX.'category', "id = '$parent_id'");
           if (!empty($result)) {
              $this->_updateLink($current_slug, $parent_id);
           }
				}
				modules::run('admincp/saveLog',$this->module,$this->db->insert_id(),'Add new','Add new');
				return true;
			}
		}else{
			if($parent_id != 0 && $this->input->post('hiddenIdAdmincp') == $parent_id){
				print('same_parent');
				return false;
			}
			$result = $this->getDetailManagement($this->input->post('hiddenIdAdmincp'));

			$data = array(
				'title_vi' => $this->input->post('titleViAdmincp'),
				'title_en' => $this->input->post('titleEnAdmincp'),
				'title_gr' => $this->input->post('titleGrAdmincp'),
				'description_vi' => $this->input->post('descVnAdmincp'),
				'description_en' => $this->input->post('descEnAdmincp'),
				'description_gr' => $this->input->post('descGrAdmincp'),
				'parent_id' =>$parent_id,
				'order_view' => $this->input->post('orderViewGrAdmincp'),
				'slug' => $this->input->post('slugAdmincp'),
				'link' => $this->input->post('slugAdmincp'),
				'status'=> $status,
				'modified'=> date('Y-m-d H:i:s',time())
			);
			modules::run('admincp/saveLog',$this->module,$this->input->post('hiddenIdAdmincp'),'','Update',$result,$data);
			$this->db->where('id',$this->input->post('hiddenIdAdmincp'));
			if($this->db->update(PREFIX.$this->table,$data)){
				//update link
				if($parent_id != 0){
					$result = $this->model->get('*', PREFIX.'category', "id = '$parent_id'");
           if (!empty($result)) {
              $this->_updateLink($current_slug, $parent_id);
           }
				}
				return true;
			}
		}
		return false;
	}

	private function _getParent($parent_id){
		$this->db->select('*');
		$this->db->where('id',$parent_id);
		$query = $this->db->get(PREFIX.'category');
		if($query->result()){
			return $query->row();
		}
		else
			return false;
	}

	function getSingleNode($current_slug){
		$this->db->select('*');
		$this->db->where('slug',$current_slug);
		$query = $this->db->get(PREFIX.'category');
		if($query->result()){
			return $query->row();
		}
		else
			return false;
	}

	private function _updateLink($current_slug = '', $parent_id = 0) {
		if($parent_id != 0){
			$parent = $this->_getParent($parent_id);
			$current = $this->getSingleNode($current_slug);
			$link = $parent->link . '/' . $current_slug;
			$data['link'] = $link;
      $this->model->update(PREFIX.'category', $data, "id = $current->id");
      //pr($this->db->last_query(),1);
		}
	}

	function list_category()
	{
		$this->db->select('*');
		$this->db->where('status',1);
		$this->db->limit(5);
		$this->db->order_by('order_view','DESC');
		$query = $this->db->get(PREFIX.'category');
		if($query->num_rows() > 0)
		{
			return $query->result();
		}
		return false;
	}

}
